
/**
 * 底盘控制基于利亚普洛夫控制律
 */

#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Spawn.h>
#include "turtlesim/Pose.h"
#include <math.h>


static double x1,yl,yaw1,pitch1,roll1,     x2,y2,yaw2,pitch2,roll2,
         	                dx,dy,dyaw       ,dpitch,droll, yaw, pitch, roll,
						    ex,ey,eyaw,         ex_t,ey_t,eyaw_t,
          	                 v1,omega1,v2,omega2,
         	                 K1,K2,K3;


static ros::Subscriber sub1;
static ros::Subscriber sub2;
static ros::Publisher turtle_vel;

void doPose1(const turtlesim::Pose::ConstPtr    & p){
	x1=p->x;
	yl=p->y;
	yaw1=p->theta;
	v1=p->linear_velocity;
	omega1=p->angular_velocity;
}

void doPose2(const turtlesim::Pose::ConstPtr& p){
	x2=p->x;
	y2=p->y;
	yaw2=p->theta;
	v2=p->linear_velocity;
	omega2=p->angular_velocity;
}

// 方法一：订阅在发布中运行

int main(int argc, char** argv)
{
	setlocale(LC_ALL,"");

	// 初始化ROS节点
	ros::init(argc, argv, "my_tf_listener");

    // 创建节点句柄
	ros::NodeHandle node;

	// 请求产生turtle3
	ros::service::waitForService("/spawn");
	ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");
	turtlesim::Spawn srv;
	add_turtle.call(srv);

	sub1 = node.subscribe<turtlesim::Pose>("/turtle1/pose",1000,doPose1);
	sub2 = node.subscribe<turtlesim::Pose>("/turtle3/pose",1000,doPose2);
	// 创建发布turtle2速度控制指令的发布者
	turtle_vel = node.advertise<geometry_msgs::Twist>("/turtle3/cmd_vel", 10);

	// 创建tf的监听器
	tf::TransformListener listener;

	ros::Rate rate(10);
	while (node.ok())
	{

		// 获取turtle1与turtle2坐标系之间的tf数据
		tf::StampedTransform transform;
		try
		{
			listener.waitForTransform("/turtle3", "/turtle1", ros::Time(0), ros::Duration(3.0));
			listener.lookupTransform("/turtle3", "/turtle1", ros::Time(0), transform);
		}

		catch (tf::TransformException &ex) 
		{
			ROS_ERROR("%s",ex.what());
			ros::Duration(1.0).sleep();
			continue;
		}

		dx=x1-x2;
		dy=yl-y2;
		dyaw=yaw1-yaw2;
        ex_t=cos(yaw2)*dx+sin(yaw2)*dy;
        ey_t=-sin(yaw2)*dx+cos(yaw2)*dy;
        eyaw_t=dyaw;

        ex=transform.getOrigin().x();
        ey=transform.getOrigin().y();
		transform.getBasis().getEulerYPR(eyaw, pitch, roll);

		ROS_INFO("乌龟1位姿信息:x=%.2f,y=%.2f,theta=%.2f,lv=%.2f,av=%.2f",
		x1,yl,yaw1,v1,omega1);
		ROS_INFO("乌龟2位姿信息:x=%.2f,y=%.2f,theta=%.2f,lv=%.2f,av=%.2f",
		x2,y2,yaw2,v2,omega2);
		ROS_INFO("乌龟位姿差:ex=%.2f,ey=%.2f,etheta=%.2f",
		transform.getOrigin().x()-ex_t,transform.getOrigin().y()-ey_t,eyaw-eyaw_t);

		// 根据turtle1与turtle3坐标系之间的位置关系，发布turtle2的速度控制指令
		geometry_msgs::Twist vel_msg;

        K1=0.909;
        K2=1.250;
        K3=0.818;

		vel_msg.linear.x = v1*cos(eyaw)+K2*ex;
		vel_msg.angular.z = omega1+ey*v1/K1+K3*sin(eyaw)/K1;



		// vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(),
		// 		                        transform.getOrigin().x());
		// vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) +
		// 		                      pow(transform.getOrigin().y(), 2));


		turtle_vel.publish(vel_msg);


		rate.sleep();
		ros::spinOnce();
	}
	return 0;
};